/**
* @description: 988. 从叶结点开始的最小字符串
* @author hewei
* @date 2022/10/4 15:09
* @version 1.0
*/

public class SmallestFromLeaf {

    String ans = "";
    public String smallestFromLeaf(TreeNode root) {
        process(root, new StringBuilder());
        return ans;
    }

    public void process(TreeNode node, StringBuilder s) {
        if (node == null) return;
        s.append((char) (node.val + 'a'));
        if (node.left == null && node.right == null) {
            String temp = s.reverse().toString();
            s.reverse();
            if (ans.equals("") || ans.compareTo(temp) > 0) ans = temp;
        }
        process(node.left, s);
        process(node.right, s);
        s.deleteCharAt(s.length() - 1);
    }
}
